package abq2.jic.jic.plugin.operate.activity;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.MobileFormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.BillList;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.report.ReportList;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

/**
 * 报表界面插件
 */
public class ShowInfo extends AbstractReportFormPlugin implements Plugin {
    String act_id = "chooseConf";
    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {

        super.beforeDoOperation(e);
        String operateKey = ((FormOperate) e.getSource()).getOperateKey();
        if(((FormOperate)e.getSource()).getOperateKey().equals("display")){

            Object primaryKeyValue = null;

            ReportList reportList = this.getControl("reportlistap");
            int[] selectedRows = reportList.getEntryState().getSelectedRows();
            for (int selectedRow : selectedRows) {
                DynamicObject rowData = reportList.getReportModel().getRowData(selectedRow);
                String abq2_number = rowData.getString("abq2_number");
                DynamicObject dy = BusinessDataServiceHelper.loadSingle("abq2_activity",
                        "number,id",
                        (new QFilter("number", QCP.equals, abq2_number)).toArray());
                primaryKeyValue = dy.getLong("id");
                break;
            }

            //弹窗界面
            FormShowParameter formShowParameter = new MobileFormShowParameter();
            formShowParameter.setFormId("abq2_activity_info");
            formShowParameter.setCustomParam("activityId",primaryKeyValue);
            formShowParameter.setCloseCallBack(new CloseCallBack(this,act_id));
            formShowParameter.getOpenStyle().setShowType(ShowType.Modal);
            this.getView().showForm(formShowParameter);
        }else if (operateKey.equals("showai")){
            String pageId = this.getView().getMainView().getPageId();
            Object pkValue = Long.parseLong("1987269179689937920");
            DispatchServiceHelper.invokeBizService("ai", "gai", "GaiService","selectProcessInSideBar",pkValue,pageId,"请输入您的偏好，我们会为您推荐相应的活动");
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent a) {
        super.closedCallBack(a);
        String actionId = a.getActionId();
        if (actionId.equalsIgnoreCase(act_id)){
            Object returnData = a.getReturnData();
            if (returnData != null){
                Boolean b = (Boolean) returnData;
                if (b){
                    this.getView().showSuccessNotification("选课成功");
                }
            }
        }
        ReportList list = this.getControl("reportlistap");
        list.refresh();
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
    }
}